/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.ZhsqCy;
import me.zhengjie.business.service.ZhsqCyService;
import me.zhengjie.business.service.dto.ZhsqCyQueryCriteria;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2024-05-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "照护存疑管理")
@RequestMapping("/api/zhsqCy")
public class ZhsqCyController {

    private final ZhsqCyService zhsqCyService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('zhsqCy:list')")
    public void exportZhsqCy(HttpServletResponse response, ZhsqCyQueryCriteria criteria) throws IOException {
        zhsqCyService.download(zhsqCyService.queryAll(criteria), response);
    }



    @GetMapping
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public ResponseEntity<Object> queryZhsqCy(ZhsqCyQueryCriteria criteria,
                                              @RequestParam(defaultValue = "0")Integer page,
                                              @RequestParam(defaultValue = "10")Integer size) {
        Long userid = SecurityUtils.getCurrentUserId();

        criteria.setBisZhsqCyztNull(true);
        if(userid != 548) {
            criteria.setUserId(userid);
        }

        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(zhsqCyService.queryZhsqCy(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/queryCqtc")
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public ResponseEntity<Object> queryCqtc(ZhsqCyQueryCriteria criteria,
                                            @RequestParam(defaultValue = "0")Integer page,
                                            @RequestParam(defaultValue = "10")Integer size) {
        Long userid = SecurityUtils.getCurrentUserId();

        criteria.setBisZhsqCyztNull(false);
        criteria.setBisZhsqCyzt("60");
        if(userid != 548) {
            criteria.setUserId(userid);
        }

        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(zhsqCyService.queryZhsqCy(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/queryCqtc/download")
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public void queryCqtcDownload(ZhsqCyQueryCriteria criteria, HttpServletResponse response) throws IOException {
        Long userid = SecurityUtils.getCurrentUserId();

        criteria.setBisZhsqCyztNull(false);
        criteria.setBisZhsqCyzt("60");
        if(userid != 548) {
            criteria.setUserId(userid);
        }

        zhsqCyService.queryZhsqCyDown(criteria, response);
    }

    @GetMapping(value = "/queryCqCy")
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public ResponseEntity<Object> queryCqCy(ZhsqCyQueryCriteria criteria,
                                            @RequestParam(defaultValue = "0")Integer page,
                                            @RequestParam(defaultValue = "10")Integer size){
        Long userid = SecurityUtils.getCurrentUserId();

        criteria.setBisZhsqCyztNull(false);
        criteria.setBisZhsqCyzt("61");
        if(userid != 548) {
            criteria.setUserId(userid);
        }

        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(zhsqCyService.queryZhsqCy(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/queryCqCy/download")
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public void queryCqCyDownload(ZhsqCyQueryCriteria criteria, HttpServletResponse response) throws IOException {
        Long userid = SecurityUtils.getCurrentUserId();

        criteria.setBisZhsqCyztNull(false);
        criteria.setBisZhsqCyzt("61");
        if(userid != 548) {
            criteria.setUserId(userid);
        }

        zhsqCyService.queryZhsqCyDown(criteria, response);
    }

    @GetMapping(value = "/queryAllCqtc")
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public ResponseEntity<Object> queryAllCqtc(ZhsqCyQueryCriteria criteria,
                                               @RequestParam(defaultValue = "0")Integer page,
                                               @RequestParam(defaultValue = "10")Integer size) {

        criteria.setBisZhsqCyztNull(false);
        criteria.setBisZhsqCyzt("60");
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(zhsqCyService.queryZhsqCy(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/queryAllCqtc/download")
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public void queryAllCqtcDownload(ZhsqCyQueryCriteria criteria, HttpServletResponse response) throws IOException {

        criteria.setBisZhsqCyztNull(false);
        criteria.setBisZhsqCyzt("60");
        zhsqCyService.queryZhsqCyDown(criteria, response);
    }

    @GetMapping(value = "/queryAllCqCy")
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public ResponseEntity<Object> queryAllCqCy(ZhsqCyQueryCriteria criteria,
                                               @RequestParam(defaultValue = "0")Integer page,
                                               @RequestParam(defaultValue = "10")Integer size) {

        criteria.setBisZhsqCyztNull(false);
        criteria.setBisZhsqCyzt("61");
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(zhsqCyService.queryZhsqCy(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/queryAllCqCy/download")
    @Log("查询照护存疑")
    @ApiOperation("查询照护存疑")
    public void queryAllCqCyDownload(ZhsqCyQueryCriteria criteria, HttpServletResponse response) throws IOException {

        criteria.setBisZhsqCyztNull(false);
        criteria.setBisZhsqCyzt("61");
        zhsqCyService.queryZhsqCyDown(criteria, response);
    }

    @PostMapping
    @Log("新增照护存疑")
    @ApiOperation("新增照护存疑")
    @PreAuthorize("@el.check('zhsqCy:add')")
    public ResponseEntity<Object> createZhsqCy(@Validated @RequestBody ZhsqCy resources){
        return new ResponseEntity<>(zhsqCyService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改照护存疑")
    @ApiOperation("修改照护存疑")
    @PreAuthorize("@el.check('zhsqCy:edit')")
    public ResponseEntity<Object> updateZhsqCy(@Validated @RequestBody ZhsqCy resources){
        zhsqCyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除照护存疑")
    @ApiOperation("删除照护存疑")
    @PreAuthorize("@el.check('zhsqCy:del')")
    public ResponseEntity<Object> deleteZhsqCy(@RequestBody Long[] ids) {
        zhsqCyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}